let wxparse = require("../../wxParse/wxParse.js")
let http = require('../../utils/api.js');
import {
    toData,
    myHttps
} from '../../utils/util.js';
import {
    myHttpss
} from '../../utils/json.js';
Page({
    data: {
        hiddenLoading: false,
        comments: [], //评论接口获取得所有数据
        total: '', //评论得数量
        fid: '', //某条评论的id
        replyId: '', //回复回复的id
        id: '', //点哪条评论 传过来哪条评论的id
        val: '', //评论输入框的内容
        contents: '', //走评论接口返回给我的数据
        huifushui: '1',
        xianshi: false, //控制发送按钮的颜色
        huii: false, //自动聚焦
        zhankai: false,
        writecomments: false, //输入内容的输入框
        themeId: '', //关注主题参数
        scrollTop: 0,
        floorstatus: false,
        articleId: '', //文章id
        massage: '',
        isFocus: '', //是否关注
        collect: false, //未收藏
        comment: false, //显示评论
        content: '', //写评论输入的内容
        showLike: true,
        goods: '', //评论点赞
        dkheight: '', //插入html的高度
        contentheight: '', //正文的高度
        uid: '',
        fromid: '',
        cid: '',
        data: '', //详情页的所有数据
        dkcontent: '', //内容区的数据  
        userId: '', //用户id
        userInfo: '', //用户信息
        nologin: '',
        isFav: '', //是否收藏
        nologin: '',
        first: '',
        likecount: '', //文章的点赞量
        reply: '', //回复接口请求到的数据
        islike: '', //是否点赞
        essaysid: '', //文章id
        page: 1, //当前页面 
        size: 10, //页容量
        toView: 'inToViewcontent',
        index: '',//评论的index
        img: '',//分享页面得图片
        imgs: '',
        contentss: '',
        titless: '',
        idss: '',
        indexcontent: '',//从首页带过来得内容部分 用于分享给朋友
        role: '',//判断是不是作者身份
        codes: false,//二维码弹框
    },

    onLoad: function (e) {
        // console.log(e)
        if (e.content) {
            this.setData({
                indexcontent: e.content,
            })
        }
        this.setData({
            articleId: e.id,
            nologin: wx.getStorageSync('no_login'),
            first: wx.getStorageSync('first'),
            userId: wx.getStorageSync('userId'),
        })
        // console.log(this.data.articleId)
        //判断是否 授权登录过 
        if (this.data.nologin == 'yes') { //没有登陆过
            this.setData({
                first: true,
            })
        } else {
            // 调用获取用户详情接口
            this.GetInfo()
        }
        // 调用 获取文章评论列表  
        this.articleComments(this.data.page, this.data.size);

    },
    // 点击确定  弹出授权登陆
    goLogin() {
        // 调用微信登陆接口 
        login()
        this.setData({
            first: false,
            userInfo: wx.getStorageSync('userInfo'),
        })
        // console.log(this.data.userInfo)
        this.GetInfo()
    },
    // 获取用户信息接口 
    GetInfo() {
        var url = http.api.userInfo + this.data.userId;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                // console.log(res.data)
                this.setData({
                    userInfo: res.data,
                    role: res.data.role
                })
            }
        }).catch(err => { })
    },
    // 评论点赞
    goods(e) {
        this.setData({
            hiddenLoading: false
        })
        // console.log(e.currentTarget.dataset.index)
        var index = e.currentTarget.dataset.index
        var id = e.currentTarget.dataset.id
        // console.log(e)
        let url = http.api.commentGoods + id + '/like/essay';
        myHttps({
            url: url,
            method: "POST",
        }).then(res => {
            // console.log(res)
            if (res.code == 1) {
                // wx.showToast({
                //     title: '点赞成功',
                //     icon: 'success',
                //     duration: 1000
                // })
                // 点赞成功后 改变小手的状态和点赞的数量
                this.data.comments[index].isLike = true
                this.data.comments[index].like_count = this.data.comments[index].like_count + 1
                this.setData({
                    comments: this.data.comments,
                    hiddenLoading: true
                })
            }
        }).catch(err => { })
    },
    // 评论取消点赞
    nogoods(e) {
        this.setData({
            hiddenLoading: false
        })
        var index = e.currentTarget.dataset.index
        var id = e.currentTarget.dataset.id
        // console.log(e)
        let url = http.api.commentGoods + id + '/like/essay';
        myHttps({
            url: url,
            method: "DELETE",
        }).then(res => {
            // console.log(res)
            if (res.code == 1) {
                // wx.showToast({
                //     title: '取消点赞',
                //     duration: 1000
                // })
                //取消点赞成功后 改变小手的状态和点赞的数量
                this.data.comments[index].isLike = false
                this.data.comments[index].like_count = this.data.comments[index].like_count - 1
                this.setData({
                    comments: this.data.comments,
                    hiddenLoading: true
                })
            }
        }).catch(err => { })

    },
    // 获取文章评论接口
    articleComments(page, size, types) {
        this.setData({
            hiddenLoading: false
        })
        let id = this.data.articleId;
        let url = http.api.reviewCommentList + id + '/comments' + '?page=' + page + '&size=' + size;
        var that = this
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            // console.log(this.data.comments)
            if (res.code == 1) {
                var arrs = res.data.content;
                if (types) {
                    arrs = arrs.reverse()
                }
                for (var i = 0; i < arrs.length; i++) {
                    res.data.content[i].insert_time = toData(res.data.content[i].insert_time); //时间格式化
                    if (types) {
                        that.data.comments.unshift(res.data.content[i])
                    } else {
                        that.data.comments.push(res.data.content[i])
                    }

                }
                //  console.log(this.data.comments)
                that.setData({
                    comments: that.data.comments,
                    // comments: res.data.content,
                    total: res.data._meta.total,
                    hiddenLoading: true
                })
            }

        }).catch(err => { })
    },
    // 上拉加载10条评论
    // onReachBottom: function () {
    //     this.setData({
    //         page: this.data.page++,
    //     })
    //     // 调用 获取文章评论列表  
    //     this.articleComments(this.data.page,this.data.size);
    // },
  
    // 关闭弹框二维码
    closess() {
        this.setData({
            codes: false, //弹框 二维码
        })
    },
    //输入字变色
    shuru(e) {
        let val = e.detail.value;
        this.setData({
            val: val
        })
        // console.log(this.data.val)
        if (e.detail.value == '') {
            this.setData({
                xianshi: false
            });
        } else {
            this.setData({
                xianshi: true
            });
        }

    },
    //关闭输入弹框
    closebg() {
        this.setData({
            writecomments: false
        })
    },
    //发送接口
    send() {
        this.setData({
            hiddenLoading: false
        })
        var that = this
        if (this.data.huifushui == 2) {
            //写评论接口
            this.getWritecomment()
        }
        else if (this.data.huifushui == 4) {
            //回复评论接口
            this.replyComments()
        }
    },
    // 写评论
    writeComment() {
        this.setData({
            huifushui: '2',
            huii: true,
            writecomments: true,
        })
    },
    // 写评论 点击完成 走接口
    getWritecomment() {
        let id = this.data.articleId;
        let that = this
        let url = http.api.reviewComment + id + '/comments';
        let obj = {
            content: this.data.val,
        }
        myHttpss({
            url: url,
            method: "POST",
            data: obj
        }).then(res => {
            if (res.code == 1) {
                this.data.comments.splice(0, 9);
                // console.log(this.data.comments)
                this.setData({
                    comments: this.data.comments,
                    toView: 'inToView' + 'pinglun',
                    comment: true,
                    hiddenLoading: true
                })
                this.articleComments(1, 10, 'u')
            }
        }).catch(err => { })
    },
    // 点击评论 出现弹窗
    replyComment(e) {
        let id = e.currentTarget.dataset.id;
        let index = e.currentTarget.dataset.index
        this.setData({
            index: index,
            id: id,
            huifushui: '4',
            huii: true,
            writecomments: true,
            fromid: e.currentTarget.dataset.id,
            uid: e.currentTarget.dataset.uid,
        })
    },
    // 点击回复评论 接口
    replyComments() {
        var that = this
        let id = this.data.articleId;
        let url = http.api.reviewComment + id + '/comments';
        let index = this.data.index
        let obj = {
            to_uid: this.data.uid,
            content: this.data.val,
            f_id: this.data.fromid
        }
        myHttpss({
            url: url,
            method: "POST",
            data: obj
        }).then(res => {
            if (res.code == 1) {
                this.data.comments.splice(0, 9);
                // console.log(this.data.comments);
                this.setData({
                    comments: this.data.comments,
                    toView: 'inToView' + 'pinglun',
                    comment: true,
                    hiddenLoading: true
                })
                this.articleComments(1, 10, 'u')
            }
        }).catch(err => { })
        this.setData({
            zhankai: true
        })
    },

    // 点击收藏 接口
    collect() {
        let id = this.data.data.id
        let url = http.api.collects + id + '/fav';
        myHttps({
            url: url,
            method: "POST",
        }).then(res => {
            if (res.code == 1) {
                this.setData({
                    // scrollTop: 0,
                    collect: true,
                    isFav: true
                })
                wx.showToast({
                    title: '收藏成功',
                    icon: 'success',
                    duration: 1000
                })
            }
        }).catch(err => { })
    },
    // 取消收藏
    cancelcollect() {
        let id = this.data.data.id
        let url = http.api.collects + id + '/fav';
        myHttps({
            url: url,
            method: "DELETE",
        }).then(res => {
            if (res.code == 1) {
                this.setData({
                    // scrollTop: 0,
                    collect: false,
                    isFav: false
                })
                // wx.showToast({
                //     title: '取消收藏',
                //     icon: 'success',
                //     duration: 1000
                // })
            }
        }).catch(err => { })
    },
    // 点击展开
    moreComment() {
        // console.log(this.data.data)
        let url = http.api.moreComment;
        let obj = {
            size: this.data.data.page.size,
            articleId: this.data.data.id,
            current: this.data.data.page.current
        }
        myHttps({
            url: url,
            method: "POST",
            data: obj
        }).then(res => {
            // console.log(res.data)
            if (res.code == 0) {
                for (var i = 0; i < res.data.length; i++) {
                    console.log(res.data[i])
                    this.data.data.commentAndReply.push(res.data[i])
                }
                this.setData({
                    data: this.data.data
                })
            }
        }).catch(err => { })
        this.setData({
            zhankai: true
        })
    },
    // 点击收起
    shouqi() {
        // console.log(this.data.data)
        let url = http.api.moreComment;
        let obj = {
            size: this.data.data.page.size,
            articleId: this.data.data.id,
            current: this.data.data.page.current
        }
        myHttps({
            url: url,
            method: "POST",
            data: obj
        }).then(res => {
            // console.log(res.data)
            if (res.code == 0) {
                for (var i = 0; i < res.data.length; i++) {
                    console.log(res.data[i])
                    this.data.data.commentAndReply.pop(res.data[i])
                }
                this.setData({
                    data: this.data.data
                })
            }
        }).catch(err => { })
        this.setData({
            zhankai: false
        })
    },
    // 弹窗打开事件 点击分享事件
    showss(e) {
        // console.log(e)
        this.setData({
            imgss: e.currentTarget.dataset.img,
            titless: e.currentTarget.dataset.title,
            idss: e.currentTarget.dataset.id,
            contentss: e.currentTarget.dataset.content,
            show: true
        });
    },
    // 分享弹窗收藏事件
    collects(e) {
        let fav = e.currentTarget.dataset.fav;
        if (fav == true) {
            this.cancelcollect() //取消收藏
        } else if (fav == false) {
            this.collect() //点击收藏
        }
        this.setData({
            show: false
        });
    },
    // 分享弹窗取消的点击事件
    cancel() {
        this.setData({
            show: false
        });
    },
    // 分享弹框的收藏
    sharecollect() {
        this.collect()
        this.cancelcollect()
    },
    // 分享 页面
    onShareAppMessage: function (res) {
        console.log(res)
        if (res.from === 'button') {
            // 来自页面内转发按钮
            this.cancel()
        } else if (res.from == 'menu') {
            return {
                title: this.data.titless,
                imageUrl: this.data.imgss,
                path: '/pages/details/details?id=' + this.data.idss
            }
        }
        // console.log(res)
        return {
            title: this.data.titless,
            imageUrl: this.data.imgss,
            path: '/pages/details/details?id=' + this.data.idss
        }

        this.setData({
            show: false
        });
    },
    // 弹窗分享到朋友圈
    Share() {
        this.setData({
            show: false
        });
        wx.showLoading({
            title: '加载中',
        })
        setTimeout(function () {
            wx.hideLoading()
        }, 2000)
        wx.navigateTo({
            url: '../picture/picture?title=' + this.data.titless + '&id=' + this.data.idss + '&content=' + this.data.indexcontent,
        })
    }
})